
var ShowBox = new Class({
    Implements: [Events, Options],
    options: {
        duration: 1,
        width: 'auto'
    },
    block:null,
    initialize: function(opts) {
        this.setOptions(opts);

        this.block = document.id('msgBox');
        if(!this.block) {
            this.block = new Element('div#msgBox').inject(document.body);
        }
    },
    say: function(messg) {
        var top = window.getScroll().y + (window.getSize().y / 2) - 72; // full size
        var left = window.getScroll().x + (window.getSize().x / 2) - 75; // half size

        this.block.set({
            'html': '<p>' + messg + '</p>',
            'styles': {
                'margin-top':0,
                'top': top + 'px',
                'left': left + 'px'
            }
        }).morph({'opacity':[0,0.9]});

        this.hide.delay(this.options.duration * 1000, this);
    },
    setPrice: function(ev) {
        if(this.block.retrieve('allow')) return;
        this.block.store('allow', true);

        var pos = ev.target.getPosition();
        var top = pos.y + 22;
        var left = pos.x - 30;

        var selPriceTag = new Element('input[type=text][value=' + ev.target.retrieve('selPrice').toFixed(2) + ']',{
            'events': {
                'click': function() { this.focus(); this.select(); }
            }
        });
        var resPriceTag = new Element('input[type=text][value=' + ev.target.retrieve('resPrice').toFixed(2) + ']', {
            'events': {
                'click': function() { this.focus(); this.select(); }
            }
        });

        var save = new Element('a[href=javascript:;][text=Save]', {
            'events': {
                'click': function(evt) {
                    evt.stop();
                    this.hide();

                    this.fireEvent('save', [
                        evt.target.retrieve('accwareSKU'),
                        evt.target.retrieve('selPriceTag').get('value'),
                        evt.target.retrieve('resPriceTag').get('value')
                    ]);
                }.bind(this),
                'focus': function() {this.blur(); }
            }
        }).store('accwareSKU', ev.target.retrieve('accware')).store('selPriceTag', selPriceTag).store('resPriceTag', resPriceTag);

        var cancel = new Element('a[href=javascript:;][text=Cancel][style=color:red;]', {
            'events': {
                'click': function() {
                    ev.stop();
                    this.hide();
                    this.fireEvent('cancel');
                }.bind(this),
                'focus': function() {this.blur();}
            }
        });


        var table = new Element('table').adopt(
            new Element('tbody').adopt(
                new Element('tr').adopt(
                    new Element('td[colspan=2][html=SKU : ' + ev.target.retrieve('accware') + '<hr/>]')
                ),
                new Element('tr').adopt(
                    new Element('td.right[text=Sell Price:]'),
                    new Element('td').adopt(selPriceTag)
                ),
                new Element('tr').adopt(
                    new Element('td.right[text=Reseller Price:]'),
                    new Element('td').adopt(resPriceTag)
                ),
                new Element('tr').adopt(
                    new Element('td.size14[colspan=2]').adopt(
                        new Element('hr'),
                        save,
                        new Element('b[html=&nbsp;&nbsp;|&nbsp;&nbsp;]'),
                        cancel
                    )
                )
            )
        );
        table.inject(
            this.block.setStyles({
                'margin-top':0,
                'text-align': 'center',
                'width': '150px',
                'height': '70px',
                'top': top + 'px',
                'left': left + 'px'
            }).morph({'opacity':[0,1.0]})
        );

    },
    getUserHistory: function(evt) {

        this.getUserHistory = new Request({
            url: '/Admin/webstat/getFromMySQL',
            method: 'get',
            onRequest: function() {
                new Loader().show();
            },
            onComplete: function(data) {
                var json;
                try {
                    json = eval(data);
                }
                catch(err) { alert('Error...' + err); return; }
                new Loader().hide();

                this.showUserHistory(evt, json);
            }.bind(this)
        }).send('table=gfeed&ex=getHistory&iid=' + evt.target.retrieve('iid') + '&nc=' + Date.now());

    },
    showUserHistory: function(evt, data) {

        if(this.block.retrieve('allow')) return;
        this.block.store('allow', true);

        var pos = evt.target.getPosition();
        var top = pos.y + 22;
        var left = pos.x - 430;

        var tbody = new Element('tbody'), color = true;
        data.each(function(user) {
            new Element('tr' + ((color = !color) ? '[style=background-color:gray;]' : '')).adopt(
                new Element('td[text=' + user.name + ']'),
                new Element('td[text=$' + numberFormat(user.oldsell) + ']'),
                new Element('td[text=$' + numberFormat(user.newsell) + ']'),
                new Element('td[text=$' + numberFormat(user.oldresl) + ']'),
                new Element('td[text=$' + numberFormat(user.newresl) + ']'),
                new Element('td[text=' + user.date + ']')
            ).inject(tbody);
        });

        var close = new Element('a[href=javascript:;][text=Close][style=color:#15adff;font-weight:bold;]', {
            'events': {
                'click': function() {
                    this.hide();
                }.bind(this),
                'focus': function() {this.blur();}
            }
        });

        var table = new Element('table[style=font-size:10px;]').adopt(
            tbody,
            new Element('tfoot').adopt(
                new Element('tr').adopt(
                    new Element('td.size14[colspan=6]').adopt(
                        new Element('hr'),
                        close
                    )
                )
            ),
            new Element('thead').adopt(
                new Element('tr').adopt(
                    new Element('th[text=User]'),
                    new Element('th[text=Old Sell Price]'),
                    new Element('th[text=New Sell Price]'),
                    new Element('th[text=Old Resl Price]'),
                    new Element('th[text=New Resl Price]'),
                    new Element('th[text=Date]')
                ),
                new Element('th[colspan=6]').adopt(
                    new Element('hr')
                )
            )
        );

        table.inject(
            this.block.setStyles({
                'margin-top':0,
                'text-align': 'center',
                'width': '500px',
                'height': 'auto',
                'top': top + 'px',
                'left': left + 'px'
            }).morph({'opacity':[0,1.0]})
        );
    },
    getDiscount: function(ev) {

        new Request({
            url: '/Admin/webstat/getFromMySQL',
            method: 'get',
            onRequest: function() {
                new Loader().show();
            },
            onComplete: function(data) {
                var json;
                try {
                    json = eval('(' + data + ')');
                }
                catch(err) { alert('Error...' + err); return; }
                new Loader().show();

                this.saveDiscount(json, ev);
            }.bind(this)
        }).send('table=gfeed&ex=getDisc&cid=' + ev.target.retrieve('cid') + '&nc=' + Date.now());
    },
    saveDiscount: function(data, tag) {

        if(this.block.retrieve('allow')) return;
        this.block.store('allow', true);

        var pos = tag.target.getPosition();
        var top = pos.y + 14;
        var left = pos.x + 80;

        var discount = new Element('input[type=text][value=' + (data.dct ? data.dct.toFixed(2) : '') + ']',{
            'events': {
                'click': function() { this.focus(); this.select(); }
            }
        });
        var apple0 = new Element('input[type=text][value=' + (data.appl0 ? data.appl0.toFixed(2) : '') + ']',{
            'events': {
                'click': function() { this.focus(); this.select(); }
            }
        });
        var apple1 = new Element('input[type=text][value=' + (data.appl1 ? data.appl1.toFixed(2): '') + ']',{
            'events': {
                'click': function() { this.focus(); this.select(); }
            }
        });
        var apple2 = new Element('input[type=text][value=' + (data.appl2 ? data.appl2.toFixed(2) : '') + ']',{
            'events': {
                'click': function() { this.focus(); this.select(); }
            }
        });
        var apple3 = new Element('input[type=text][value=' + (data.appl3 ? data.appl3.toFixed(2) : '') + ']',{
            'events': {
                'click': function() { this.focus(); this.select(); }
            }
        });
        var apple4 = new Element('input[type=text][value=' + (data.appl4 ? data.appl4.toFixed(2) : '') + ']',{
            'events': {
                'click': function() { this.focus(); this.select(); }
            }
        });
        var apple5 = new Element('input[type=text][value=' + (data.appl5 ? data.appl5.toFixed(2) : '') + ']',{
            'events': {
                'click': function() { this.focus(); this.select(); }
            }
        });

        var save = new Element('a[href=javascript:;][text=Save]', {
            'events': {
                'click': function(ev) {
                    ev.stop();
                    this.hide();

                    new Request({
                        url: '/Admin/webstat/getFromMySQL',
                        method: 'get',
                        onRequest: function() {
                            new ShowBox().say('Save discount...');
                            new Loader().show();
                        },
                        onComplete: function() {
                            new Loader().hide();
                        }
                    }).send(
                        'table=gfeed&ex=saveDisc' +
                        '&cid=' + data.cid +
                        '&sid=' + data.sid +
                        '&dct=' + discount.get('value') +
                        '&a0=' + apple0.get('value') +
                        '&a1=' + apple1.get('value') +
                        '&a2=' + apple2.get('value') +
                        '&a3=' + apple3.get('value') +
                        '&a4=' + apple4.get('value') +
                        '&a5=' + apple5.get('value') +
                        '&nc=' + Date.now());
                }.bind(this),
                'focus': function() {this.blur(); }
            }
        });

        var cancel = new Element('a[href=javascript:;][text=Cancel][style=color:red;]', {
            'events': {
                'click': function(ev) {
                    ev.stop();
                    this.hide();
                }.bind(this),
                'focus': function() {this.blur();}
            }
        });

        var table = new Element('table').adopt(
            new Element('tbody').adopt(
                new Element('tr').adopt(
                    new Element('td[colspan=2][html=&nbsp;]')
                ),
                new Element('tr').adopt(
                    new Element('td[colspan=2][html=<hr/>]')
                ),
                new Element('tr').adopt(
                    new Element('td.right[text=discount:]'),
                    new Element('td').adopt(discount)
                ),
                new Element('tr').adopt(
                    new Element('td').adopt(
                        new Element('div').adopt(
                            new Element('img[src=../webstat/img/apple.png][style=top:0;]')
                        )
                    ),
                    new Element('td').adopt(apple0)
                ),
                new Element('tr').adopt(
                    new Element('td').adopt(
                        new Element('div').adopt(
                            new Element('img[src=../webstat/img/apple.png][style=top:-50px;]')
                        )
                    ),
                    new Element('td').adopt(apple1)
                ),
                new Element('tr').adopt(
                    new Element('td').adopt(
                        new Element('div').adopt(
                            new Element('img[src=../webstat/img/apple.png][style=top:-100px;]')
                        )
                    ),
                    new Element('td').adopt(apple2)
                ),
                new Element('tr').adopt(
                    new Element('td').adopt(
                        new Element('div').adopt(
                            new Element('img[src=../webstat/img/apple.png][style=top:-150px;]')
                        )
                    ),
                    new Element('td').adopt(apple3)
                ),
                new Element('tr').adopt(
                    new Element('td').adopt(
                        new Element('div').adopt(
                            new Element('img[src=../webstat/img/apple.png][style=top:-200px;]')
                        )
                    ),
                    new Element('td').adopt(apple4)
                ),
                new Element('tr').adopt(
                    new Element('td').adopt(
                        new Element('div').adopt(
                            new Element('img[src=../webstat/img/apple.png][style=top:-250px;]')
                        )
                    ),
                    new Element('td').adopt(apple5)
                ),
                new Element('tr').adopt(
                    new Element('td.size14[colspan=2]').adopt(
                        new Element('hr'),
                        save,
                        new Element('b[html=&nbsp;&nbsp;|&nbsp;&nbsp;]'),
                        cancel
                    )
                )
            )
        );
        table.inject(
            this.block.setStyles({
                'margin-top':0,
                'text-align': 'center',
                'width': '150px',
                'height': '180px',
                'top': top + 'px',
                'left': left + 'px'
            }).morph({'opacity':[0,1.0]})
        );

    },
    hide: function() {
        this.block.morph({'opacity': 0});

        this.destroy.delay(this.options.duration * 1000, this);
    },
    destroy: function() {
        this.block.destroy();
    }
});
